package com.nuotu.datawall.clouddata.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nuotu.datawall.clouddata.R;
import com.nuotu.datawall.clouddata.util.FinalStr;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * <b>Copyright:</b>  Copyright © 2011-2014 . All Rights Reserved<br/>
 * <b>Description:</b> 自定义[设置界面]适配器 <br/>
 * <b>author:</b>  zcf <br/>
 * <b>data:</b>  2016/1/4 18:05<br/>
 *
 * @version :  V1.0 <br/>
 */
public class SettingActivityLVAdapter extends BaseAdapter {
    private ArrayList<HashMap<String, Object>> data;
    private Context mContext;
    private ListView mListView;
    //    private LayoutInflater inflater;
    final int VIEW_TYPE = 2;    //item类型的总数
    final int TYPE_IV = 0;  //包含imageview的item视图
    final int TYPE_TV = 1;  //包含textview的item视图

    public SettingActivityLVAdapter(Context context, ArrayList<HashMap<String, Object>> list) {
        mContext = context;
        data = list;

    }

    /**
     * 设置listview对象
     *
     * @param lisv
     */
    public void setListView(ListView lisv) {
        this.mListView = lisv;
    }

    public class ViewHolder1 {
        TextView title1;    //标题
        ImageView image1;   //图片
    }

    public class ViewHolder2 {
        TextView title2;    //标题
        TextView descr2;    //描述
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE;
    }

    @Override
    public int getItemViewType(int position) {
        if (data.get(position).get(FinalStr.SETTING_ACT_TYPE_KEY)
                .toString().equals("" + FinalStr.SETTING_ACT_TYPE_IV)) {
            return TYPE_IV;
        } else if (data.get(position).get(FinalStr.SETTING_ACT_TYPE_KEY)
                .toString().equals("" + FinalStr.SETTING_ACT_TYPE_TV)) {
            return TYPE_TV;
        } else {
            return 100;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder1 holder1 = null;
        ViewHolder2 holder2 = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            // 按当前所需的样式，确定new的布局
            switch (type) {
                case TYPE_IV:
                    convertView = inflater.inflate(R.layout.act_setting_lv_item_iv, parent, false);
                    holder1 = new ViewHolder1();
                    holder1.title1 = (TextView) convertView
                            .findViewById(R.id.act_setting_lv_item_iv_tv_word);
                    holder1.image1 = (ImageView) convertView
                            .findViewById(R.id.act_setting_lv_item_iv_tv_img);
                    convertView.setTag(holder1);
                    break;
                case TYPE_TV:
                    convertView = inflater.inflate(R.layout.act_setting_lv_item_tv, parent, false);
                    holder2 = new ViewHolder2();
                    holder2.title2 = (TextView) convertView
                            .findViewById(R.id.act_setting_lv_item_2tv_word);
                    holder2.descr2 = (TextView) convertView
                            .findViewById(R.id.act_setting_lv_item_2tv_right);
                    convertView.setTag(holder2);
                    break;
            }
        } else {
            switch (type) {
                case TYPE_IV:
                    holder1 = (ViewHolder1) convertView.getTag();
                    break;
                case TYPE_TV:
                    holder2 = (ViewHolder2) convertView.getTag();
                    break;
            }
        }
        try {
            //填充信息
            switch (type) {
                case TYPE_IV:
                    holder1.title1.setText(data.get(position)
                            .get(FinalStr.SETTING_ACT_WORD_KEY).toString());
                    holder1.image1.setImageResource(Integer.parseInt(data.get(position)
                            .get(FinalStr.SETTING_ACT_DESC_IMG_KEY).toString()));
                    break;
                case TYPE_TV:
                    holder2.title2.setText(data.get(position)
                            .get(FinalStr.SETTING_ACT_WORD_KEY).toString());
                    holder2.descr2.setText(data.get(position)
                            .get(FinalStr.SETTING_ACT_DESC_KEY).toString());
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }
}
